<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2022\3\22 0022
 * Time: 15:45
 */

/*
 * 目的是删除掉重复的数据，只留最新的
 */

$array=array(
    array(
        'lab_combo_id'=>1,
        'time'=>'2020-10-12 11:23:59'
    ),
    array(
        'lab_combo_id'=>1,
        'time'=>'2020-10-12 11:22:59'
    ),
    array(
        'lab_combo_id'=>3,
        'time'=>'2020-10-12 11:22:59'
    ),
    array(
        'lab_combo_id'=>3,
        'time'=>'2020-10-12 11:23:59'
    ),
);
foreach($array as $value){
   $lab_combo_id= $value['lab_combo_id'];
}



//按照多个字段进行排序
function sortArrByManyField(){
    $args = func_get_args();
    if(empty($args)){
        return null;
    }
    $arr = array_shift($args);
    if(!is_array($arr)){
        throw new Exception("第一个参数不为数组");
    }
    foreach($args as $key => $field){
        if(is_string($field)){
            $temp = array();
            foreach($arr as $index=> $val){
                $temp[$index] = $val[$field];
            }
            $args[$key] = $temp;
        }
    }
    $args[] = &$arr;//引用值
    call_user_func_array('array_multisort',$args);
    return array_pop($args);
}
$data=sortArrByManyField($array,'lab_combo_id',SORT_ASC,'time',SORT_DESC);
echo "<pre>";
var_dump($data);
echo "</pre>";

//删除重复的批次
function unsetRepeatBatchData($data,$index)
{
    $existIndex = [];
    foreach ($data as $key => $value) {
        if (in_array($value[$index], $existIndex)) {
            unset($data[$key]);
            continue;
        }
        $existIndex[] = $value[$index];
    }
    $data = array_merge($data);
    return $data;
}

$data=unsetRepeatBatchData($data,'lab_combo_id');
echo "<pre>";
var_dump($data);
echo "</pre>";
